if not HST then return end

local L = HST.L

local _, race = UnitRace("player")
HST.Race = race

function HST:PlayerIsTroll()
	return HST.Race == "Troll"
end

function HST:UI_ERROR_MESSAGE()
	-- print("UI_ERROR_MESSAGE", arg1)
end

function HST:DELAY_SHOT()
	self.casting = true
end

function HST:UPDATE_PLAYER_POSITION()
	local x, y = GetPlayerMapPosition("player")
	if x ~= self.x or y ~= self.y then
		self.moving = true
		self.x = x
		self.y = y
	else
		self.moving = nil
	end
end

function HST:UNIT_AURA(arg1)
	if arg1 == "player" and self:PlayerIsTroll() then 
		self:HandleBerserk()
	elseif arg1 == "target" and self:EWCoverageEnabled() then 
		self:EWHandleTargetAuraChanged()
	end
end

function HST:PLAYER_REGEN_DISABLED()
	if self:EWCoverageEnabled() then
		self:EWHandleEnterBattle()
	end
end

function HST:PLAYER_REGEN_ENABLED()
	if self:EWCoverageEnabled() then 
		self:EWHandleLeaveBattle()
	end
end

function HST:START_AUTOREPEAT_SPELL()
	self:BarOnEvent("START_AUTOREPEAT_SPELL", arg1)
end

function HST:STOP_AUTOREPEAT_SPELL()
	self:BarOnEvent("STOP_AUTOREPEAT_SPELL", arg1)
	self:ApiOnEvent("STOP_AUTOREPEAT_SPELL", arg1)
end

function HST:HST_ON_BAR_UPDATE()
	self:BarOnEvent("HST_ON_BAR_UPDATE", nil)
end

function HST:HST_ON_CAST_BAR_UPDATE()
	self:CastBarOnEvent("HST_ON_CAST_BAR_UPDATE", nil)
end

function HST:CHAT_MSG_SPELL_DAMAGESHIELDS_ON_SELF()
	if string.find(arg1, L["HST_MISS"]) then
		self:Announce(HST.db.profile.tranqfailmsg)
	end
end

function HST:UNIT_RANGEDDAMAGE()
	self:BarOnEvent("UNIT_RANGEDDAMAGE")
	self:ApiOnEvent("UNIT_RANGEDDAMAGE")
end

function HST:SPELLCAST_DELAYED()
	self:CastBarOnEvent("SPELLCAST_DELAYED", arg1)
end

function HST:SPELLCAST_INTERRUPTED()
	self:CastBarOnEvent("SPELLCAST_INTERRUPTED")
	self:ApiOnEvent("SPELLCAST_INTERRUPTED")
	self:CancelScheduledEvent("HST_TRANQ")
end

function HST:SPELLCAST_FAILED()
	self:CastBarOnEvent("SPELLCAST_FAILED")
	self:ApiOnEvent("SPELLCAST_FAILED")
	self:CancelScheduledEvent("HST_TRANQ")
end

function HST:PLAYER_CONTROL_LOST()
	self:CancelScheduledEvent("HST_TRANQ")
end

function HST:SPELLCAST_START()
	self:CastBarOnEvent("SPELLCAST_START", arg1, arg2)
end

function HST:SPELLCAST_STOP()
	self:CastBarOnEvent("SPELLCAST_STOP", arg1)
	self:ApiOnEvent("SPELLCAST_STOP", arg1)
	
	if incTranq and HST.db.profile.channel and HST.db.profile.channel ~= "" then
		local msg = string.gsub(HST.db.profile.tranqmsg, "%%t", currTarget)
		self:Announce(msg)
	end
end

function HST:SPELLCAST_CHANNEL_START()
	self:CastBarOnEvent("SPELLCAST_CHANNEL_START", arg1, arg2)
end

function HST:SPELLCAST_CHANNEL_UPDATE()
	self:CastBarOnEvent("SPELLCAST_CHANNEL_UPDATE", arg1)
end

function HST:SPELLCAST_CHANNEL_STOP()
	self:CastBarOnEvent("SPELLCAST_CHANNEL_STOP", arg1)
end

function HST:PLAYER_ENTERING_WORLD()
	HST:InitEquippedAmmoCount()
	HST:SetAreaEvents()
end

function HST:ZONE_CHANGED_NEW_AREA()
	HST:SetAreaEvents()
end

local SPECIAL_EVENTS = {}

function HST:SetAreaEvents()
	SPECIAL_EVENTS = HST.RaidEvents[GetRealZoneText()] or {}
end

function HST:CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE()
	for index in SPECIAL_EVENTS do
		for source in gfind(arg1,SPECIAL_EVENTS[index]) do
			if IsAutoRepeatAction(23) then
				-- 默认ClassicMacros将自动射击放入动作条23的位置
				UseAction(23)
			end
		end
	end
end

function HST:ITEM_LOCK_CHANGED()
	-- Block events that is not caused by hunter shooting and spells.
	if self:BlockNonAmmoEvents(arg1) then
		return
	end

	-- Hunter shooting spells also consume ammos, while autoshots
	-- wait for spells to finish first. So spells have higher
	-- priority over autoshot to respond to this event
	if self:CastBarOnEvent("ITEM_LOCK_CHANGED") then return end

	self:BarOnEvent("ITEM_LOCK_CHANGED", arg1)
	self:ApiOnEvent("ITEM_LOCK_CHANGED", arg1)
end

function HST:SendBarUpdateEvent()
	if not self:IsEventScheduled("HST_ON_BAR_UPDATE") then
		self:ScheduleRepeatingEvent("HST_ON_BAR_UPDATE", self.HST_ON_BAR_UPDATE, 0, self)
	end
end

function HST:CancelBarUpdateEvent()
	self:CancelScheduledEvent("HST_ON_BAR_UPDATE")
end

function HST:SendCastBarUpdateEvent()
	if not self:IsEventScheduled("HST_ON_CAST_BAR_UPDATE") then
		self:ScheduleRepeatingEvent("HST_ON_CAST_BAR_UPDATE", self.HST_ON_CAST_BAR_UPDATE, 0, self)
	end
end

function HST:CancelCastBarUpdateEvent()
	self:CancelScheduledEvent("HST_ON_CAST_BAR_UPDATE")
end

function HST:SendPlayerMovingEvent()
	self.x, self.y = GetPlayerMapPosition("player")
	self:ScheduleRepeatingEvent("HST_UPDATE_PLAYER_POSITION", self.UPDATE_PLAYER_POSITION, 0.1, self)
end

function HST:CancelPlayerMovingEvent()
	self:CancelScheduledEvent("HST_UPDATE_PLAYER_POSITION")
end